<?php
namespace app\pay\controller\common;

use think\Controller;

class DaifuYuE extends Controller {
    /*
     * 客户向畅远管理后台发起代付申请
     * 将申请信息保存到model('PayBisApply')
     */
    public function index() {
        $data = input('post.');
        // 根据商户号获取商户相关信息
        $resPayBis = model('PayBis')->get(['merchant_id' => $data['merchant_id']]);
        if($resPayBis) {
            $res = $this->queryHandle($data, $resPayBis);
            return $res;
        } else {
            // 商户号不存在
            return paymchntIdFalse();
        }  
    }
    
    /*
     * 验证签名并保存数据到数据库
     */
    private function queryHandle($data, $resPayBis) {
        $verSign = $data['signature'];
        unset($data['signature']);
        $md5key = $resPayBis->md5key;
        $data['signature'] = payChangyuanSign($data, $md5key); // 创建签名
        // 验证签名
        if($verSign == $data['signature']) {        
            $dataArr = array(
                'merchant_id'    => $resPayBis->merchant_id,
                'amount'          => $resPayBis->amount,
                'djamount'        => $resPayBis->djamount,
            );
            return daifuYuETrue($dataArr, $resPayBis->md5key); 
        } else {
            // <畅远>签名验证失败
            return changyuanSignFalse();
        } 
    }
}

